﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OreSystem.BLL;
using OreSystem.Entities;
namespace MainFrom
{
    public partial class ItemForm : Form
    {
        public int Type { get; set; } //汽车或挖机
        private ItemBLL itemBLL = new ItemBLL();
        public ItemForm()
        {
            InitializeComponent();
        }

        private void ItemForm_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;

            dtp_datekeys.Format = DateTimePickerFormat.Custom;
            dtp_datekeys.CustomFormat = "yyyy-MM-dd";

            dataGridView1.Columns.Add("ID", "序号");
            dataGridView1.Columns.Add("Type", "类型");
            dataGridView1.Columns.Add("CarNumber", "车编号");
            dataGridView1.Columns.Add("times", "次数");
            if (this.Type == 1)
            {
                dataGridView1.Columns.Add("Lenght", "距离");
            }
            else
            {
                dataGridView1.Columns.Add("CalType", "装载类型");
            }
            dataGridView1.Columns.Add("CreateDate", "添加时间");
            dataGridView1.Columns[0].DataPropertyName = "ID";
            dataGridView1.Columns[1].DataPropertyName = "Type";
            dataGridView1.Columns[2].DataPropertyName = "CarNumber";
            dataGridView1.Columns[3].DataPropertyName = "times";
            if (this.Type == 1)
            {
                dataGridView1.Columns[4].DataPropertyName = "Lenght";
            }
            else if (this.Type == 2)
            {
                dataGridView1.Columns[4].DataPropertyName = "CalType";
            }
            dataGridView1.Columns[5].DataPropertyName = "CreateDate";            
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.MultiSelect = false;
            dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;            
            dataGridView1.DataSource = itemBLL.GetItemByType(this.Type);
            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);

            //dataGridView1.AutoResizeColumns();
            //dataGridView1.Width = dataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Displayed)+150;
            //this.Width = dataGridView1.Width + 100;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ItemEdit itemEditFrom = new ItemEdit();
            itemEditFrom.Type = this.Type;
            itemEditFrom.NewItem = new Item();
            if (itemEditFrom.ShowDialog()== System.Windows.Forms.DialogResult.OK)
            {
                dataGridView1.DataSource = itemBLL.GetItemByType(this.Type);
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            ItemEdit itemEditFrom = new ItemEdit();
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show("请选择要编辑的数据！");
                return;
            }
            itemEditFrom.NewItem = itemBLL.GetItemById((int)dataGridView1.CurrentRow.Cells[0].Value);
            itemEditFrom.Type = this.Type;
            if (itemEditFrom.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                dataGridView1.DataSource = itemBLL.GetItemByType(this.Type);
            }
        }

        private void btn_search_Click(object sender, EventArgs e) {
            dataGridView1.DataSource = itemBLL.GetItemsByKeys(this.Type, txt_keys.Text, dtp_datekeys.Value);
        }

        private void toolStripButton3_Click(object sender, EventArgs e) {
            if (dataGridView1.CurrentRow == null) {
                MessageBox.Show("请选择要删除的数据！");
                return;                
            }
            if (MessageBox.Show("确定删除吗？", "删除提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK) {
                try {
                    int id = (int)dataGridView1.CurrentRow.Cells[0].Value;
                    itemBLL.Delete(itemBLL.GetItemById(id));
                    dataGridView1.DataSource = itemBLL.GetItemByType(this.Type);
                }
                catch (Exception ex) {

                    MessageBox.Show("出错了，请联系管理员！错误信息:" + ex.Message);
                }
            }
            
        }

        private void button1_Click(object sender, EventArgs e) {
           
        }
    }
}
